﻿Imports System.Globalization
Imports System.Collections.ObjectModel

Namespace ViewModel

    Public Class LiveProgramViewModel
        Inherits ViewModelBase

        Private Model As Model.LiveProgram

        Public Sub New(ByVal model As Model.LiveProgram)
            Me.Model = model
        End Sub

        Public Function ToModel() As Model.LiveProgram
            Model.Station.PluginGuids.Clear()
            For Each a In Station.AlertManners
                If a.IsEnabled Then
                    Model.Station.PluginGuids.Add(a.Plugin.Guid)
                End If
            Next
            Return Model
        End Function

#Region "Wrapper"

        Public ReadOnly Property Id() As Integer
            Get
                Return Model.Id
            End Get
        End Property

        Public Property Caster() As String
            Get
                Return If(Model.CasterName, "")
            End Get
            Set(ByVal value As String)
                Model.CasterName = value
                OnPropertyChanged("Caster")
            End Set
        End Property

        Public Property Title() As String
            Get
                Return Model.Title
            End Get
            Set(ByVal value As String)
                Model.Title = value
                OnPropertyChanged("Title")
                OnPropertyChanged("TitleSubset")
            End Set
        End Property

        Public Property Content() As String
            Get
                Return Model.Content
            End Get
            Set(ByVal value As String)
                Model.Content = value
                OnPropertyChanged("Content")
            End Set
        End Property

        Public Property ShortContent() As String
            Get
                Return Model.ShortContent.Replace(vbCr, "").Replace(vbLf, "")
            End Get
            Set(ByVal value As String)
                Model.ShortContent = value
                OnPropertyChanged("ShortContent")
            End Set
        End Property

        Public Property VisitorCount() As Integer
            Get
                Return Model.VisitorCount
            End Get
            Set(ByVal value As Integer)
                Model.VisitorCount = value
                OnPropertyChanged("VisitorCount")
            End Set
        End Property

        Public Property CommentCount() As Integer
            Get
                Return Model.CommentCount
            End Get
            Set(ByVal value As Integer)
                Model.CommentCount = value
                OnPropertyChanged("CommentCount")
            End Set
        End Property

        Public ReadOnly Property IsMemberOnly() As Boolean
            Get
                Return Model.IsMemberOnly
            End Get
        End Property

        Public ReadOnly Property StartDateTime() As DateTime
            Get
                Return Model.StartDateTime
            End Get
        End Property

        Private WithEvents _Station As StationViewModel
        Public ReadOnly Property Station() As StationViewModel
            Get
                If _Station Is Nothing Then
                    _Station = New StationViewModel(Model.Station)
                End If
                Return _Station
            End Get
        End Property

        Public ReadOnly Property OfficialTag() As String
            Get
                Return Model.OfficialTag
            End Get
        End Property

        Public ReadOnly Property IsFace() As Boolean
            Get
                Return Model.IsFace
            End Get
        End Property

        Public ReadOnly Property CrawlingDateTime() As DateTime
            Get
                Return Model.CrawlingDateTime
            End Get
        End Property

        Public ReadOnly Property Uri() As Uri
            Get
                Return Model.Uri
            End Get
        End Property

        Public Property IsReserved() As Boolean
            Get
                Return Model.IsReserved
            End Get
            Set(ByVal value As Boolean)
                Model.IsReserved = value
                OnPropertyChanged("IsReserved")
            End Set
        End Property

        Public Property IsOnAir() As Boolean
            Get
                Return Model.IsOnAir
            End Get
            Set(ByVal value As Boolean)
                Model.IsOnAir = value
                OnPropertyChanged("IsOnAir")
            End Set
        End Property

#End Region

        Private _IsAlerted As Boolean
        Public Property IsAlerted() As Boolean
            Get
                Return _IsAlerted
            End Get
            Set(ByVal value As Boolean)
                _IsAlerted = value
                OnPropertyChanged("IsAlerted")
            End Set
        End Property

        ' 通知対象時、前回の放送情報をもっているか
        Private _hasRecentInformation As Boolean
        Public Property HasRecentInformation() As Boolean
            Get
                Return _hasRecentInformation
            End Get
            Set(ByVal value As Boolean)
                _hasRecentInformation = value
                OnPropertyChanged("HasRecentInformation")
            End Set
        End Property



#Region "For Xceed DataGrid"
        Public ReadOnly Property Self() As LiveProgramViewModel
            Get
                Return Me
            End Get
        End Property

        Private _Group As String = ""
        Public Property Group() As String
            Get
                Return _Group
            End Get
            Set(ByVal value As String)
                _Group = value
            End Set
        End Property

        'Private _TitleSubset As LiveProgramTitleSubsetViewModel
        'Public ReadOnly Property TitleSubset() As LiveProgramTitleSubsetViewModel
        '    Get
        '        If _TitleSubset Is Nothing Then
        '            _TitleSubset = New LiveProgramTitleSubsetViewModel(Model)
        '        End If
        '        Return _TitleSubset
        '    End Get
        'End Property

        Public Property AlertManners() As Integer
            Get
                Return Station.AlertManners.Count
            End Get
            Set(ByVal value As Integer)
                ' Ignore
            End Set
        End Property

        Private _Icon As IconViewModel
        Public ReadOnly Property Icon() As IconViewModel
            Get
                If _Icon Is Nothing Then
                    _Icon = New IconViewModel(Me)
                End If
                Return _Icon
            End Get
        End Property

        Private Sub _Station_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles _Station.PropertyChanged
            Select Case e.PropertyName
                Case "Icon"
                    OnPropertyChanged("Icon")
                Case "Level"
                    OnPropertyChanged("Level")
                Case "MemberCount"
                    OnPropertyChanged("MemberCount")
                Case "Name"
                    OnPropertyChanged("StationName")
                Case "UpdateDateTime"
                    OnPropertyChanged("UpdateDateTime")
            End Select
        End Sub

        Public ReadOnly Property Level() As Integer
            Get
                Return Station.Level
            End Get
        End Property

        Public ReadOnly Property MemberCount() As Integer
            Get
                Return Station.MemberCount
            End Get
        End Property

        Public ReadOnly Property StationName() As String
            Get
                Return Station.Name
            End Get
        End Property

        Public ReadOnly Property UpdateDateTime() As DateTime
            Get
                Return Station.UpdateDateTime
            End Get
        End Property

#End Region

        'Private WithEvents _tags As New ObservableCollection(Of TagViewModel)
        'Public ReadOnly Property Tags() As ObservableCollection(Of TagViewModel)
        '    Get
        '        Return _tags
        '    End Get
        'End Property

        'Public ReadOnly Property FilterdTags() As IList(Of String)
        '    Get
        '        Dim tags = From t In Me.Tags Order By t.Kind = TagKind.Caster, t.Kind = TagKind.User, t.Kind = TagKind.Self _
        '                   Select t.Value Distinct

        '        Return tags.ToArray
        '    End Get
        'End Property

        'Private Sub _tags_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles _tags.CollectionChanged
        '    OnPropertyChanged("FilterdTags")
        'End Sub

        'Private _subset As LiveProgramSubsetViewModel
        'Public Property Subset() As LiveProgramSubsetViewModel
        '    Get
        '        If _subset Is Nothing Then
        '            _subset = New LiveProgramSubsetViewModel(LiveProgramModel)
        '        End If
        '        Return _subset
        '    End Get
        '    Set(ByVal value As LiveProgramSubsetViewModel)
        '        _subset = value
        '        OnPropertyChanged("Subset")
        '    End Set
        'End Property



    End Class
End Namespace
